package com.decent.manager.system.controller;

import com.decent.common.bo.MessageBean;
import com.decent.common.enums.ErrorCodeEnum;
import com.decent.manager.system.dto.SystemBannerDTO;
import com.decent.manager.system.service.SystemBannerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author: decent Gjh
 * @date: 2022/10/24/14:09
 * @description:
 */
@RequestMapping("systemBanner")
@RestController
@Slf4j
@CrossOrigin
public class SystemBannerController {
    private final SystemBannerService bannerService;

    public SystemBannerController(SystemBannerService bannerService) {
        this.bannerService = bannerService;
    }

    /**
     * 上传banner
     * method:post
     * args：
     *   file file 文件
     *   bannerType	string 跳转类型
     *   value string 跳转参数
     *   sort int 顺序
     * @param banner 参数信息
     * @param result result
     * @return 是否成功
     */
    @PostMapping()
    public MessageBean upload(@Validated(Insert.class) SystemBannerDTO banner, BindingResult result){
        if (result.hasErrors()){
            return new MessageBean(ErrorCodeEnum.INVALID_PARAMS, result.getAllErrors().get(0).getDefaultMessage());
        }
        return bannerService.insertBanner(banner);
    }

    /**
     * 修改banner
     * method:put
     * args：
     *   id int
     *   sort int 排序
     *   value string 跳转参数
     *   bannerType	string 跳转类型
     *   imageUrl string 图片链接
     * @param banner 参数信息
     * @param result result
     * @return 是否成功
     */
    @PutMapping()
    public MessageBean update(@Validated(Update.class) SystemBannerDTO banner, BindingResult result){
        if (result.hasErrors()){
            return new MessageBean(ErrorCodeEnum.INVALID_PARAMS, result.getAllErrors().get(0).getDefaultMessage());
        }
        return bannerService.updateBanner(banner);
    }

    /**
     * 删除banner
     * args：
     *    id int
     * @param banner  banner参数
     * @param result result
     * @return 是否成功
     */
    @DeleteMapping()
    public MessageBean delete(@Validated(Delete.class) SystemBannerDTO banner, BindingResult result) {
        if (result.hasErrors()){
            return new MessageBean(ErrorCodeEnum.INVALID_PARAMS, result.getAllErrors().get(0).getDefaultMessage());
        }
        return bannerService.deleteBanner(banner.getId());
    }

    /**
     * 获取所有banner
     * @return 包含banner列表的messageBean
     */
    @GetMapping()
    public MessageBean getBanner(){
        return bannerService.getBanner();
    }

}
